package com.wiloon.java8x.basic.event;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by wiloon on 4/23/17;
 */
public class DoorManager {
    private Collection listeners;

    /**
     * 添加事件
     *
     * @param listener DoorListener
     */
    void addDoorListener(DoorListener listener) {
        if (listeners == null) {
            listeners = new HashSet();
        }
        listeners.add(listener);
    }

    /**
     * 移除事件
     *
     * @param listener DoorListener
     */
    public void removeDoorListener(DoorListener listener) {
        if (listeners == null) return;
        listeners.remove(listener);
    }

    /**
     * 触发开门事件
     */
    void fireWorkspaceOpened() {
        if (listeners == null) return;
        DoorEvent event = new DoorEvent(this, "open");
        notifyListeners(event);
    }

    /**
     * 触发关门事件
     */
    void fireWorkspaceClosed() {
        if (listeners == null) return;
        DoorEvent event = new DoorEvent(this, "close");
        notifyListeners(event);
    }

    /**
     * 通知所有的DoorListener
     */
    private void notifyListeners(DoorEvent event) {
        Iterator iterator = listeners.iterator();
        while (iterator.hasNext()) {
            DoorListener listener = (DoorListener) iterator.next();
            listener.doorEvent(event);
        }
    }
}